//
//  GooglePlusMediaShareCommunity.m
//  MediaShareBB
//
//  Created by Artak Gevorgyan on 5/23/13.
//  Copyright (c) 2013 InnoriseEntertainment. All rights reserved.
//

#import "GooglePlusMediaShareCommunity.h"
#import "MediaShareData.h"
#import "MediaShareBBUtilityMethods.h"

//#import <GooglePlus/GooglePlus.h>
//#import <GoogleOpenSource/GoogleOpenSource.h>

@interface GooglePlusMediaShareCommunity()/*<GPPSignInDelegate, GPPShareDelegate> */{
	NSString* _clientId;
	NSArray* _friendsList;
	MediaShareData* _sharedMediaData;
}

@end

@implementation GooglePlusMediaShareCommunity

-(id) init {
	self = [super init];
	if (self) {
		super.name = @"Google Plus";
		super.communityType = MEDIA_SHARE_COMMUNITY_GOOGLE_PLUS;
		super.supportedMediaTypes = [[NSArray alloc] initWithObjects:
									 [NSNumber numberWithInt:IMAGE_MEDIA_TYPE],
									 [NSNumber numberWithInt:URL_MEDIA_TYPE], nil];
		_friendsList = [[NSArray alloc] init];
	}
	return self;
}

#pragma mark - Superclass methods

// Superclass method
-(NSString*) description {
	NSString* d = @"Google+ description";
	return d;
}

// Superclass method
-(void) configureWithCredentials:(NSDictionary*) credentials {
	_clientId = [credentials valueForKey:@"clientId"];
}

// Superclass method
-(void) login {
//	GPPSignIn *signIn = [GPPSignIn sharedInstance];
//	signIn.clientID = _clientId;
//	if (![self isSignedIn]) {
//		[self signIn];
//	}
}


//Superclass method
-(void) loginWithAnotherUser {
}

//Superclass method
-(NSString*) getUserName {
    return @"";
}

//Superclass method
-(NSString*) sharedMediaLink {
    return @"";
}

// Superclass method
-(void) shareMedia:(MediaShareData*) data {
	if ([super isValidMediaType:data.mediaType]) {
		_sharedMediaData = data;
		if ([MediaShareBBUtilityMethods isObject:super.delegate
							  respondsToSelector:@selector(willSharingMedia:through:)]) {
			[super.delegate willSharingMedia:_sharedMediaData
									 through:self];
		}
	} else {
		NSLog(@"%s: Unsupported media type:%d", __PRETTY_FUNCTION__, data.mediaType);
	}
//	GDataFeedPhotoAlbum* albumFeedOfPhotos = [self photoFeed];
//	NSURL *uploadURL = [[albumFeedOfPhotos uploadLink] URL];
//	[self addAPhotoToUploadURL:uploadURL];
}

/*
#pragma mark - GPPSignInDelegate methods

- (void)finishedWithAuth: (GTMOAuth2Authentication *)auth
                   error: (NSError *) error
{
	if (error != nil) {
		NSLog(@"%s: Login failed with error:%@", __PRETTY_FUNCTION__, error);
	} else {
		NSLog(@"%s: Login success: Auth object:%@", __PRETTY_FUNCTION__, auth);
	}
}

#pragma mark - GPShareDialogDelegate methods

- (void)finishedSharing:(BOOL)shared {
	NSLog(@"%s: %@", __PRETTY_FUNCTION__, shared ? @"Share success" : @"Share failed");
}

#pragma mark - Private methods

-(void) shareLink {
	id<GPPShareBuilder> shareBuilder = [[GPPShare sharedInstance] shareDialog];
//	[shareBuilder setURLToShare:[NSURL
//								 URLWithString:@"https://www.apple.com"]];

	NSString *imagePathString = [[NSBundle mainBundle] pathForResource:@"Default"
																ofType:@"png"];
//	NSURL *imageUrl = [[NSURL alloc] initWithString:@"http://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png"];
	NSURL *imageUrl = [NSURL fileURLWithPath:imagePathString];
	[shareBuilder setTitle:@"Some title" description:@"Some description"
			  thumbnailURL:imageUrl];
	[shareBuilder setContentDeepLinkID:@"post"];
	[GPPShare sharedInstance].delegate = self;
	[shareBuilder open];
}

-(void) getListOfFriends {
	[self checkAuthStatus];
	GTMOAuth2Authentication *auth = [GPPSignIn sharedInstance].authentication;
	if (!auth) {
		// To authenticate, use Google+ sign-in button.
		NSLog(@"%s: Not authenticated", __PRETTY_FUNCTION__);
		return;
	}
	GTLQueryPlus *query = [GTLQueryPlus queryForPeopleListWithUserId:@"me"
														  collection:kGTLPlusCollectionVisible];
	[[[GPPSignIn sharedInstance] plusService] executeQuery:query
										 completionHandler:^(GTLServiceTicket *ticket,
															 GTLPlusPeopleFeed *peopleFeed,
															 NSError *error) {
					 if (error) {
						 GTMLoggerError(@"Error: %@", error);
						 NSLog(@"%s: %@", __PRETTY_FUNCTION__,
										[NSString stringWithFormat:@"Status: Error: %@", error]);
					 } else {
						 _friendsList = peopleFeed.items;
						 GTLPlusPerson *person = _friendsList[0];
						 NSString *name = person.displayName;
						 NSLog(@"%s: count= %d, name = %@", __PRETTY_FUNCTION__,
															_friendsList.count,
															name);
						 NSLog(@"%s: friends list = %@", __PRETTY_FUNCTION__, _friendsList);
					 }
	 }];
}

-(void) checkAuthStatus {
	if (![GPPSignIn sharedInstance].authentication) {
		return;
	}
	if ([[GPPSignIn sharedInstance].scopes containsObject:kGTLAuthScopePlusLogin]) {
		NSLog(@"%s: Authenticated with plus", __PRETTY_FUNCTION__);
	} else {
		// To authenticate, use Google+ sign-in button.
		NSLog(@"%s: Not authenticated with plus", __PRETTY_FUNCTION__);
	}
}

-(BOOL) isSignedIn {
    return [GPPSignIn sharedInstance].authentication != nil;
}

-(void) signIn {
	GPPSignIn *signIn = [GPPSignIn sharedInstance];
	// You previously set kClientID in the "Initialize the Google+ client" step
	signIn.clientID = _clientId;
	signIn.scopes = [NSArray arrayWithObjects:
					 kGTLAuthScopePlusLogin, // defined in GTLPlusConstants.h
					 nil];
	signIn.delegate = self;
	if (![signIn trySilentAuthentication]) {
		[signIn authenticate];
	}
}

-(void) fetchPeopleImages {
	
}*/

@end
